package com.abc.scholarship.controller;


import cn.hutool.core.bean.BeanUtil;
import com.abc.scholarship.common.enums.StatusEnum;
import com.abc.scholarship.domain.dto.CollegeDTO;
import com.abc.scholarship.domain.dto.MajorDTO;
import com.abc.scholarship.domain.entity.College;
import com.abc.scholarship.domain.entity.Major;
import com.abc.scholarship.domain.page.PageDTO;
import com.abc.scholarship.domain.page.PageQuery;
import com.abc.scholarship.domain.vo.resp.ApiResult;
import com.abc.scholarship.service.CollegeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 学院表 前端控制器
 * </p>
 *
 * @author jzw
 * @since 2024-07-02
 */
@RestController
@RequestMapping("/college")
@RequiredArgsConstructor
@Api(tags = "学院相关接口")
public class CollegeController {

    private final CollegeService collegeService;

    /**
     * 查询所有学院
     * @return
     */
    @GetMapping("/getList")
    @ApiOperation("查询所有学院")
    public ApiResult<List<College>> getList() {
        return ApiResult.success(collegeService.list());
    }

    /**
     * 分页查询所有学院
     * @param page
     * @return
     */
    @GetMapping("/selectAll")
    @ApiOperation("分页查询所有学院")
    public ApiResult<PageDTO<College>> selectAll(PageQuery page) {
        Page<College> collegePage = collegeService.page(page.toMpPage());
        return ApiResult.success(PageDTO.of(collegePage, College.class));
    }

    /**
     * 根据 id查询学院
     * @param id
     * @return
     */
    @GetMapping("/select/{id}")
    @ApiOperation("id查询学院")
    public ApiResult<College> selectById(@PathVariable Integer id) {
        return ApiResult.success(collegeService.getById(id));
    }

    /**
     * 修改数据
     *
     * @param collegeDTO 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    @ApiOperation("修改学院信息")
    public ApiResult update(@RequestBody CollegeDTO collegeDTO) {
        collegeService.MUpdateById(BeanUtil.toBean(collegeDTO, College.class));
        return ApiResult.success();
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @ApiOperation("删除学院")
    @PutMapping("/delete")
    public ApiResult delete(@RequestParam("idList") List<Long> idList) {
        this.collegeService.setStatusByIds(idList, StatusEnum.NORMAL.getValue());
        return ApiResult.success("删除成功");
    }

    /**
     * 新增数据
     * @param collegeDTO
     * @return
     */
    @ApiOperation("新增学院")
    @PostMapping("/insert")
    public ApiResult insert(@RequestBody CollegeDTO collegeDTO) {
        if (collegeDTO.getCollegeName() == null || collegeDTO.getCollegeName().isEmpty()) {
            return ApiResult.fail("不能为空");
        }
        collegeService.save(BeanUtil.toBean(collegeDTO, College.class));
        return ApiResult.success("新增成功");
    }

}

